﻿using System;
using System.Collections.Generic;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using POS.BusinessEntities;

namespace POS.DataObjectsTest
{
    [TestClass]
    public static class Helper
    {
        #region Declarations

        private static Database db;
        //private const string TestUser = "testUser";
        private const int EmpresaId = 1;

        #endregion

        #region Properties: Non-Public

        private static Database Db
        {
            get
            {
                if (db == null)
                    db = DatabaseFactory.CreateDatabase();
                return db;
            }
        }

        #endregion

        #region Test Utilities

        [AssemblyInitialize]
        public static void MyAssemblyInitialize(TestContext testContext)
        {
            CleanupTestData();
            CreateTestEmpresa();
        }

        [AssemblyCleanup]
        public static void MyAssemblyCleanup()
        {
            CleanupTestData();
        }

        private static void CleanupTestData()
        {
            Db.ExecuteNonQuery(CommandType.Text, "delete contacto where empresaId = " + EmpresaId);
            Db.ExecuteNonQuery(CommandType.Text, "delete estacion where empresaId = " + EmpresaId);
            Db.ExecuteNonQuery(CommandType.Text, "delete linea where empresaId = " + EmpresaId);
            Db.ExecuteNonQuery(CommandType.Text, "delete marca where empresaId = " + EmpresaId);
            Db.ExecuteNonQuery(CommandType.Text, "delete impuesto where empresaId = " + EmpresaId);
            Db.ExecuteNonQuery(CommandType.Text, "delete proveedor where empresaId = " + EmpresaId);
            Db.ExecuteNonQuery(CommandType.Text, "delete tipoProveedor where empresaId = " + EmpresaId);
            Db.ExecuteNonQuery(CommandType.Text, "delete articulo where empresaId = " + EmpresaId);
            Db.ExecuteNonQuery(CommandType.Text, "delete empresa where empresaId = " + EmpresaId);

        }

        public static void CreateTestEmpresa()
        {
            var sql = "insert empresa (empresaId, nombre, fechaCreacion, fechaModificacion, modificadoPor) " +
                      "values (" + EmpresaId + ",'testEmpresa', getdate(), getdate(), 'TestUser')";
            Db.ExecuteNonQuery(CommandType.Text, sql);
        }

        public static void AssertGetListResult<T>(DataSet ds, IList<T> list) where T : new()
        {
            var entity = new T();
            var properties = entity.GetType().GetProperties();

            Assert.AreEqual(ds.Tables[0].Rows.Count, list.Count);
            for (var i = 0; i < list.Count; i++)
            {
                foreach (var prop in properties)
                {
                    if (ds.Tables[0].Columns.Contains(prop.Name))
                    {
                        object value;
                        var cellValue = ds.Tables[0].Rows[i][prop.Name];
                        if (prop.PropertyType == typeof(TipoContacto))
                            value = StringToTipoContacto(cellValue.ToString());
                        else if (prop.PropertyType == typeof(int?))
                            value = cellValue;
                        else
                            value = Convert.ChangeType(cellValue, prop.PropertyType);

                        Assert.AreEqual(prop.GetValue(list[i], null), value);
                    }   
                }
            }
        }
        #endregion

        private static TipoContacto StringToTipoContacto(string tipo)
        {
            switch (tipo)
            {
                case "C":
                    return TipoContacto.Clientes;
                case "P":
                    return TipoContacto.Proveedores;
            }
            return TipoContacto.Clientes; //default
        }
    }
}
